package edu.mju.filter;


import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author:wyp
 * @Date:2020/6/1 18:23
 */
@WebFilter(urlPatterns = {"/backindex"})
public class BackFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletResponse.setContentType("text/html;charSet=GBK");
        System.out.println("B拦截器成功");
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        String servletPath = request.getServletPath();
        String userRole = String.valueOf(request.getSession().getAttribute("userRole"));

        if("tourAdministration".equals(userRole)){
            filterChain.doFilter(servletRequest,servletResponse);
        }else {
            System.out.println("servletPath:"+servletPath);
            PrintWriter out = servletResponse.getWriter();
            out.print("<script>alert('对不起，您的权限不够！'); window.location='./login' </script>");
            out.flush();
            out.close();
        }
    }

    @Override
    public void destroy() {

    }
}
